@import scala.collection.mutable.Buffer 
@import controllers.gsn.auth.GSNGroup 
@import models.gsn.auth._
@(vs: Buffer[models.gsn.auth.DataSource], groups: Buffer[models.gsn.auth.Group], users: Buffer[models.gsn.auth.User], count:Int, page:Int, pageLength:Int) 
@import helper._ 
@implicitFieldConstructor = @{ FieldConstructor(twitterBootstrapBasic.f) }

@main(Messages("gsn.access.vs.title"),"vslist") {
<div id="groups" class="row">
	<div class="col-md-12">
		<h1>@Messages("gsn.access.vs.title")</h1>
	</div>
</div>
<div id="groups" class="row">
	<div class="col-xs-12 col-md-12">
		<table class="table table-striped">
			<thead>
				<tr>
					<th>@Messages("gsn.access.vs.title")</th>
					<th>@Messages("gsn.access.vs.allowed.read.users")</th>
					<th>@Messages("gsn.access.vs.allowed.read.groups")</th>
					<th>@Messages("gsn.access.vs.allowed.write.users")</th>
					<th>@Messages("gsn.access.vs.allowed.write.groups")</th>
					<th>@Messages("gsn.access.vs.allowed.all")</th>
				</tr>
			</thead>
			<tbody>
				@for(v <- vs) {
				<tr id="row_@v.id" @if(v.is_public){ class="success" }>
					<td>@v.value</td>
					<td>
						<ul class="list-unstyled hideable">
							@for(u <- v.userRead) {
							<li>@u.user.firstName @u.user.lastName (@u.user.email)
								<button type="button" class="btn btn-danger btn-xs"
									onclick="$.post('@controllers.gsn.auth.routes.PermissionsController.removefromvs(page)?vs_id=' + @v.id + '&id=ur' + @u.user.id, function(data){document.location='@controllers.gsn.auth.routes.PermissionsController.vs(page)'});">@Messages("gsn.access.users.remove")</button>
							</li> } @if(v.userRead.size < users.size){
							<li><select id="ur_@v.id"> @for(u <- users) {
									@if(null == UserDataSourceRead.findByBoth(u,v)){
									<option value="ur@u.id">@u.firstName @u.lastName
										(@u.email)</option> } }
							</select>
								<button type="button" class="btn btn-primary btn-xs"
									onclick="$.post('@controllers.gsn.auth.routes.PermissionsController.addtovs(page)?vs_id=' + @v.id + '&id=' + $('#ur_@v.id').val(), function(data){document.location='@controllers.gsn.auth.routes.PermissionsController.vs(page)'});">@Messages("gsn.access.groups.add")</button>
							</li> }
						</ul>
					</td>
					<td>
						<ul class="list-unstyled  hideable">
							@for(g <- v.groupRead) {
							<li>[ @g.group.getName ]
								<button type="button" class="btn btn-danger btn-xs"
									onclick="$.post('@controllers.gsn.auth.routes.PermissionsController.removefromvs(page)?vs_id=' + @v.id + '&id=gr' + @g.group.id, function(data){document.location='@controllers.gsn.auth.routes.PermissionsController.vs(page)'});">@Messages("gsn.access.users.remove")</button>
							</li>
							<span class="text-muted">
								<ul class="list-inline">
									@for(u <- g.group.users){
									<li>@u.firstName @u.lastName (@u.email)</li> }
								</ul>
							</span>
							</li> } @if(v.groupRead.size < groups.size){
							<li><select id="gr_@v.id"> @for(g <- groups) {
									@if(null == GroupDataSourceRead.findByBoth(g,v)){
									<option value="gr@g.id">[ @g.name ]</option> } }
							</select>
								<button type="button" class="btn btn-primary btn-xs"
									onclick="$.post('@controllers.gsn.auth.routes.PermissionsController.addtovs(page)?vs_id=' + @v.id + '&id=' + $('#gr_@v.id').val(), function(data){document.location='@controllers.gsn.auth.routes.PermissionsController.vs(page)'});">@Messages("gsn.access.groups.add")</button>
							</li> }
						</ul>
					</td>
					<td>
						<ul class="list-unstyled hideable">
							@for(u <- v.userWrite) {
							<li>@u.user.firstName @u.user.lastName (@u.user.email)
								<button type="button" class="btn btn-danger btn-xs"
									onclick="$.post('@controllers.gsn.auth.routes.PermissionsController.removefromvs(page)?vs_id=' + @v.id + '&id=uw' + @u.user.id, function(data){document.location='@controllers.gsn.auth.routes.PermissionsController.vs(page)'});">@Messages("gsn.access.users.remove")</button>
							</li> } @if(v.userWrite.size < users.size){
							<li><select id="uw_@v.id"> @for(u <- users) {
									@if(null == UserDataSourceWrite.findByBoth(u,v)){
									<option value="uw@u.id">@u.firstName @u.lastName
										(@u.email)</option> } }
							</select>
								<button type="button" class="btn btn-primary btn-xs"
									onclick="$.post('@controllers.gsn.auth.routes.PermissionsController.addtovs(page)?vs_id=' + @v.id + '&id=' + $('#uw_@v.id').val(), function(data){document.location='@controllers.gsn.auth.routes.PermissionsController.vs(page)'});">@Messages("gsn.access.groups.add")</button>
							</li> }
						</ul>
					</td>
					<td>
						<ul class="list-unstyled  hideable">
							@for(g <- v.groupWrite) {
							<li>[ @g.group.getName ]
								<button type="button" class="btn btn-danger btn-xs"
									onclick="$.post('@controllers.gsn.auth.routes.PermissionsController.removefromvs(page)?vs_id=' + @v.id + '&id=gw' + @g.group.id, function(data){document.location='@controllers.gsn.auth.routes.PermissionsController.vs(page)'});">@Messages("gsn.access.users.remove")</button>
							</li>
							<span class="text-muted">
								<ul class="list-inline">
									@for(u <- g.group.users){
									<li>@u.firstName @u.lastName (@u.email)</li> }
								</ul>
							</span>
							</li> } @if(v.groupWrite.size < groups.size){
							<li><select id="gw_@v.id"> @for(g <- groups) {
									@if(null == GroupDataSourceWrite.findByBoth(g,v)){
									<option value="gw@g.id">[ @g.name ]</option> } }
							</select>
								<button type="button" class="btn btn-primary btn-xs"
									onclick="$.post('@controllers.gsn.auth.routes.PermissionsController.addtovs(page)?vs_id=' + @v.id + '&id=' + $('#gw_@v.id').val(), function(data){document.location='@controllers.gsn.auth.routes.PermissionsController.vs(page)'});">@Messages("gsn.access.groups.add")</button>
							</li> }
						</ul>
					</td>
					<td>
					<input type="checkbox" @if(v.is_public) { checked="checked" } data-id="@v.id" onchange="check(this);"/>
					</td>
				</tr>
				}
			</tbody>
		</table>
		@paginate(page, pageLength, count, controllers.gsn.auth.routes.PermissionsController.vs(_))
	</div>
</div>
<script type="text/javascript">
function check(item){
	var i = $(item);
	if (i[0].checked){
		$.post('@controllers.gsn.auth.routes.PermissionsController.addtovs(page)?vs_id=' + i.data('id') + '&id=a', function(data){$('#row_'+i.data('id')).toggleClass('success',true)})
	}else{
		$.post('@controllers.gsn.auth.routes.PermissionsController.removefromvs(page)?vs_id=' + i.data('id') + '&id=a', function(data){$('#row_'+i.data('id')).toggleClass('success',false)})
	}
}
$('select').select2();
</script>
}
